import CustomAssembler from "./CustomAssembler";

const { ccclass, property, menu } = cc._decorator;

@ccclass
@menu('CustomRender/CustomRender')
export default class CustomRender extends cc.RenderComponent {

    /** 纹理 */
    @property({ type: cc.Texture2D, tooltip: '纹理' })
    texture: cc.Texture2D = null;

    @property({ displayName: "每条边上的顶点数量" })
    public pointsCount: number = 10;

    protected _initedMaterial: boolean = false

    onEnable() {
        super.onEnable();
        this.init();

        if (!this._assembler) { return; }
        //@ts-ignore
        this._assembler.updateRenderData(this);
    }

    public init() {
        if (!this._initedMaterial) {
            this._updateMaterial();
        }
        this.setVertsDirty();
    }

    // override
    public _resetAssembler() {
        let assembler = this._assembler = new CustomAssembler();
        assembler.init(this);
    }

    /** Update material properties */
    protected _updateMaterial() {
        let material = this.getMaterial(0);
        if (material) {
            material.define('CC_USE_MODEL', 1);
            material.setProperty('texture', this.texture);
        }
    }

    protected updateMaterial() {
        this._updateMaterial();
        this._initedMaterial = true;
    }

    public getPointCount() {
        return this.pointsCount;
    }
}